package com.sise.recomder;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sise.bean.OrderMasterAndOrderDetailForScoreBean;
import com.sise.entity.ProductBrowseRecord;
import com.sise.entity.ProductCollect;
import com.sise.entity.ProductInfo;
import com.sise.entity.User;
import com.sise.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zj
 */
@Service
public class UserProductScore {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Resource
    private ProductBrowseRecordMapper productBrowseRecordMapper;

    @Resource
    private ProductCollectMapper productCollectMapper;

    @Resource
    private OrderMasterMapper orderMasterMapper;

    public Object[][] getUserProductScoreArray() {

        List<User> userList = userMapper.selectList(null);
        List<ProductInfo> productInfoList = productInfoMapper.selectList(null);

        Integer userNumber = userList.size();
        Integer productNumber = productInfoList.size();

        Object[][] userProductScoreArray = new Object[userNumber * productNumber + 1][8];

        // 初始化数组
        int arrayRow = 1;
        for (User user : userList) {

            for (ProductInfo productInfo : productInfoList) {
                userProductScoreArray[arrayRow][1] = user.getOpenId();
                userProductScoreArray[arrayRow][2] = productInfo.getProductId();

                // 浏览
                QueryWrapper<ProductBrowseRecord> productBrowseRecordQueryWrapper = new QueryWrapper<>();
                productBrowseRecordQueryWrapper.lambda()
                        .eq(ProductBrowseRecord::getOpenId, user.getOpenId())
                        .eq(ProductBrowseRecord::getProductId, productInfo.getProductId());
                ProductBrowseRecord productBrowseRecord = productBrowseRecordMapper.selectOne(productBrowseRecordQueryWrapper);

                Double browseScore = 0.0;

                if (productBrowseRecord != null) {
                    Double timeWeight = getTimeWeight(productBrowseRecord.getUpdateTime());

                    // 浏览算分 浏览次数*行为分值*行为权重*时间权重
                    browseScore = productBrowseRecord.getBrowseCount() * 1 * 0.22 * timeWeight;
                }

                // 收藏
                QueryWrapper<ProductCollect> productCollectQueryWrapper = new QueryWrapper<>();
                productCollectQueryWrapper.lambda()
                        .eq(ProductCollect::getOpenId, user.getOpenId())
                        .eq(ProductCollect::getProductId, productInfo.getProductId());
                ProductCollect productCollect = productCollectMapper.selectOne(productCollectQueryWrapper);

                Double collectScore = 0.0;

                if (productCollect != null) {

                    // 收藏算分 行为分值*行为权重
                    collectScore = 1 * 3 * 0.41;
                }

                // 购买 & 用户评分
                List<OrderMasterAndOrderDetailForScoreBean> orderMasterAndOrderDetailForScoreBeanList = orderMasterMapper.getOrderMasterAndOrderDetailForScoreBean(user.getOpenId(), productInfo.getProductId());
                Double buyScore = 0.0;
                Double orderProductScore = 0.0;

                for (OrderMasterAndOrderDetailForScoreBean orderMasterAndOrderDetailForScoreBean : orderMasterAndOrderDetailForScoreBeanList) {

                    Double timeWeight = getTimeWeight(orderMasterAndOrderDetailForScoreBean.getCreateTime());

                    // 购买算分 行为分值*行为权重*时间权重
                    buyScore = buyScore + 5 * 1.3 * timeWeight;

                    // 用户评分算分 用户评分*行为分值*行为权重*时间权重
                    orderProductScore = orderProductScore + orderMasterAndOrderDetailForScoreBean.getOrderProductScore() * 7 * 2.0 * timeWeight;

                }

                userProductScoreArray[arrayRow][3] = browseScore + collectScore + buyScore + orderProductScore;

                arrayRow++;
            }
        }

        return userProductScoreArray;
    }

    private Double getTimeWeight(Date parameterDate) {
        Date currentDate = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

        String currentDay = simpleDateFormat.format(currentDate);

        String afterParameterDateFormat = simpleDateFormat.format(parameterDate);

        int timeDifference = Integer.parseInt(currentDay) - Integer.parseInt(afterParameterDateFormat);

        if (0 <= timeDifference && timeDifference <= 1) {
            return 1.0;
        } else if (2 <= timeDifference && timeDifference <= 3) {
            return 0.8;
        } else if (4 <= timeDifference && timeDifference <= 5) {
            return 0.6;
        } else if (6 <= timeDifference && timeDifference <= 7) {
            return 0.4;
        } else if (8 <= timeDifference && timeDifference <= 9) {
            return 0.2;
        } else {
            return 0.1;
        }
    }
}
